package com.lianniu.mgr.supplyManage.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.lianniu.mgr.base.BaseResult;
import com.lianniu.mgr.constants.ErrorCode;
import com.lianniu.mgr.pagination.PaginationInfo;
import com.lianniu.mgr.supplyManage.dao.ISupplyManageDao;
import com.lianniu.mgr.supplyManage.service.ISupplyManageService;
import com.lianniu.mgr.supplyManage.vo.SupplyApproveVo;
import com.lianniu.mgr.supplyManage.vo.SupplyConditionVo;
import com.lianniu.mgr.supplyManage.vo.SupplyVo;
import com.lianniu.mgr.util.StringUtil;

@Service
public class SupplyManageServiceImpl implements ISupplyManageService{
  private static Logger logger = LoggerFactory.getLogger(SupplyManageServiceImpl.class);
  @Autowired
  ISupplyManageDao supplyManageDao;
  
  @Override
  public BaseResult getSupplyListForPage(SupplyConditionVo condition, PaginationInfo paginationInfo) {
    condition.setPaginationInfo(paginationInfo);
    List<SupplyVo> supplyList = supplyManageDao.getSupplyListForPage(condition);
    Map<String, Object> retmap = new HashMap<String, Object>();
    retmap.put("total", paginationInfo.getTotalRecord());
    retmap.put("curPage", paginationInfo.getCurPage());
    retmap.put("rows", supplyList);

    return BaseResult.success(retmap);
  }

  @Override
  public BaseResult deleteSupply(String supplyids) {
    if(StringUtil.isEmpty(supplyids)){
      return BaseResult.fail(ErrorCode.ParaCheckErrorRetCode, ErrorCode.ParaCheckErrorRetInfo);
    }
    String[] idArr = supplyids.split(",");
    for(String id : idArr){
      supplyManageDao.deleteSupply(id);
    }
    
    return BaseResult.success();
  }
  private static String[] approveList = new String[]{"","已经注册","审核通过","审核拒绝","冻结"};
  @Override
  public BaseResult checkSupply(String status, String supplyids,String operator) {
    if(StringUtil.isEmpty(supplyids)){
      return BaseResult.fail(ErrorCode.ParaCheckErrorRetCode, ErrorCode.ParaCheckErrorRetInfo);
    }
    String[] idArr = supplyids.split(",");
    for(String id : idArr){
      SupplyApproveVo approve = new SupplyApproveVo();
      approve.setSupplyID(id);
      approve.setApprover(operator);
      approve.setDescription(approveList[Integer.valueOf(status)]);
      supplyApprove(approve);
      supplyManageDao.checkSupply(status, id);
    }
    return BaseResult.success();
  }

  @Override
  public BaseResult getSupplyById(String id) {
    SupplyVo supply = supplyManageDao.getSupplyById(id);
    return BaseResult.success(supply);
  }
  
  public BaseResult updateSupply(SupplyVo supply){
    supplyManageDao.updateSupply(supply);
    return BaseResult.success();
  }

  @Override
  public BaseResult supplyApprove(SupplyApproveVo vo) {
    supplyManageDao.supplyApprove(vo);
    return BaseResult.success();
  }

  @Override
  public BaseResult getSupplyApproveList(String supplyID,PaginationInfo paginationInfo) {
    List<SupplyApproveVo> list = supplyManageDao.getSupplyApproveListForPage(supplyID,paginationInfo);
    Map<String, Object> retmap = new HashMap<String, Object>();
    retmap.put("total", paginationInfo.getTotalRecord());
    retmap.put("curPage", paginationInfo.getCurPage());
    retmap.put("rows", list);
    return BaseResult.success(retmap);
  }

@Override
public BaseResult getSupplyNameList() {
	
	return BaseResult.success(supplyManageDao.getSupplyNameList());
	
}

@Override
public BaseResult getSupplyIdByMobile(String mobile) {
  logger.info("getSupplyIdByMobile begin");
    return BaseResult.success(supplyManageDao.getSupplyIdByMobile(mobile));
}

@Override
public boolean isExistMobile(String mobile) {
    logger.info("SupplyManageServiceImpl isExistMobile begin.");
    logger.info("input data check begin.");
    if (StringUtil.isEmpty(mobile)) {
        return false;
    }
    logger.info("input data check end.");

    logger.info("isExistMobile begin.");
    boolean isExistFlg = supplyManageDao.isExistMobile(mobile);
    boolean retFlg = isExistFlg ? true : false;
    logger.info("isExistMobile end.");

    logger.info("SupplyManageServiceImpl isExistMobile end.");
    return retFlg;
}
}
